package com.project.controller;

import com.project.dto.ReportIdNameDto;
import com.project.dto.ReportReviewListDTO;
import com.project.dto.SubmitReviewDTO;
import com.project.model.entity.ProjectReport;
import com.project.model.entity.ReportAnnotation;
import com.project.result.Result;
import com.project.service.ExportService;
import com.project.service.ProjectReportService;
import com.project.service.ReportAnnotationService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/projectReport")
public class ProjectReportController {
    private final ProjectReportService projectReportService;
    private final ReportAnnotationService reportAnnotationService;
    @Autowired
    private ExportService exportService;
    @Autowired
    public ProjectReportController(ProjectReportService projectReportService,ReportAnnotationService reportAnnotationService) {
        this.projectReportService = projectReportService;
        this.reportAnnotationService=reportAnnotationService;
    }
    @PostMapping("saveOrUpdate")
    public Result<ProjectReport> saveOrUpdateProjectReport(@RequestBody ProjectReport projectReport) {
        try {
            if (projectReport == null) {
                return Result.fail("报告内容为空");
            }

            if (projectReportService.getProjectReportById(projectReport.getId()) == null) {
                ProjectReport savedReport = projectReportService.addProjectReport(projectReport);
                return savedReport != null ? Result.ok(savedReport) : Result.fail("保存失败");
            } else {
                ProjectReport updatedReport = projectReportService.updateProjectReport(projectReport);
                return updatedReport != null ? Result.ok(updatedReport) : Result.fail("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }


    @DeleteMapping("delete")
    public Result<Void> deleteProjectReport(@RequestParam Integer id) {
        if (id == null || id <= 0) {
            return Result.fail("ID值无效");
        }
        projectReportService.deleteProjectReport(id);
        return Result.ok();
    }

    @GetMapping("getById")
    public Result<ProjectReport> getProjectReportById(@RequestParam Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            ProjectReport projectReport = projectReportService.getProjectReportById(id);
            return projectReport != null ? Result.ok(projectReport) : Result.fail("未找到报告");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @GetMapping("getAll")
    public Result<List<ProjectReport>> getAllProjectReports() {
        List<ProjectReport> reports = projectReportService.getAllProjectReports();
        return reports != null && !reports.isEmpty() ? Result.ok(reports) : Result.fail("未找到报告");
    }

    @GetMapping("getReportUrlById")
    public Result<String> getReportUrlById(@RequestParam Integer id) {
        try {
            if (id == null || id <= 0) {
                return Result.fail("ID值无效");
            }
            ProjectReport projectReport = projectReportService.getProjectReportById(id);
            System.out.println(projectReport.getReportUrl());
            return Result.ok(projectReport.getReportUrl());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统异常");
        }
    }

    @PostMapping("submitReportReview")
    public Result<Void> submitReportReview(@RequestBody SubmitReviewDTO submitReviewDTO) {
        try {
            Integer reportId=submitReviewDTO.getReportId();
            String text=submitReviewDTO.getText();
            String status=submitReviewDTO.getStatus();
            String annotationStatus="已完成";
            System.out.println(reportId+"   "+text+"   "+status);
            if(reportId==null||reportId<=0){
                return Result.fail("ID值无效");
            }
            projectReportService.updateProjectReportStatus(reportId,status);
            ReportAnnotation reportAnnotation=reportAnnotationService.getReviewingAnnotationByReportId(reportId);
            if(reportAnnotation==null){
                ReportAnnotation annotation=new ReportAnnotation();
                annotation.setReportId(reportId);
                annotation.setText(text);
                annotation.setStatus(annotationStatus);
                annotation.setReviewer("reviewer:1416");
                annotation.setAnnotationId(0);
                reportAnnotationService.saveOrUpdateAnnotation(annotation);
            }else{
                reportAnnotation.setReportId(reportId);
                reportAnnotation.setText(text);
                reportAnnotation.setStatus(annotationStatus);
                reportAnnotation.setReviewer("reviewer:1416");
                reportAnnotation.setAnnotationId(0);
                reportAnnotationService.saveOrUpdateAnnotation(reportAnnotation);
            }
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("系统异常");
        }

    }

    @GetMapping ("/getReportReviewList")
    public Result<List<ReportReviewListDTO>> getReportReviewList() {
        try {
            List<ReportReviewListDTO> reportReviewListDTOS1 = projectReportService.getReportReviewList("待评审");
            for(ReportReviewListDTO reviewListDTO:reportReviewListDTOS1){
                String reportId=String.valueOf(reviewListDTO.getId());
                projectReportService.checkDraft(reportId);
            }
            List<ReportReviewListDTO> reportReviewListDTOS=projectReportService.getReportReviewList("未通过");
            return Result.ok(reportReviewListDTOS);
        }
        catch (Exception e){
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }



    @GetMapping("/getReportIdAndReportName")
    public Result<List<ReportIdNameDto>> getReportIdAndReportName() {
        List<ReportIdNameDto> reports = projectReportService.getReportIdName();
        return reports != null && !reports.isEmpty() ? Result.ok(reports) : Result.fail("未找到报告");
    }

    @GetMapping("getAllChecked")
    public Result<List<ProjectReport>> getAllCheckedProjectReports() {
        List<ProjectReport> reports = projectReportService.getAllProjectReports();

        // 如果 reports 不为空，则进行过滤
        if (reports != null && !reports.isEmpty()) {
            // 使用 Stream API 过滤 status 为 "已审核" 的报告
            List<ProjectReport> filteredReports = reports.stream()
                    .filter(report -> "已通过".equals(report.getStatus())) // 根据 status 筛选
                    .collect(Collectors.toList());

            // 返回过滤后的结果，如果结果为空，也给出相应提示
            return !filteredReports.isEmpty()
                    ? Result.ok(filteredReports)
                    : Result.fail("未找到已审核的报告");
        }

        // 如果 reports 为空或不存在
        return Result.fail("未找到报告");
    }
}

